/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.colswe.solestac.view;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import org.colswe.solestac.VO.RequestVO;
import org.colswe.solestac.VO.StudentVO;
import org.colswe.solestac.control.RequestControl;

/**
 *
 * @author solid
 */
public class RequestForm extends javax.swing.JPanel {

    /**
     * Creates new form RequestForm
     */
    private Principal parent;
    private RequestControl rqc;
    private Date date;
    private StudentVO student;
    
    public RequestForm(Principal parent,StudentVO student) {
        this.parent = parent;        
        this.student = student;
        rqc = new RequestControl();        
        date = new Date();      
        initComponents();
    }

    
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        typeLbl = new javax.swing.JLabel();
        typeCB = new javax.swing.JComboBox();
        periodLbl = new javax.swing.JLabel();
        periodTF = new javax.swing.JTextField();
        documentsLbl = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        documentsTA = new javax.swing.JTextArea();
        createButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));

        typeLbl.setFont(new java.awt.Font("Bitstream Vera Sans", 0, 16)); // NOI18N
        typeLbl.setText("Tipo de Solicitud");

        List<String> types = rqc.getAllRequestTypes();
        typeCB = new JComboBox(types.toArray());
        typeCB.setModel(new javax.swing.DefaultComboBoxModel(types.toArray()));

        periodLbl.setFont(new java.awt.Font("Bitstream Vera Sans", 0, 16)); // NOI18N
        periodLbl.setText("Periodo Académico");

        documentsLbl.setText("Documentos de Soporte");

        documentsTA.setColumns(20);
        documentsTA.setFont(new java.awt.Font("Bitstream Vera Sans", 0, 16)); // NOI18N
        documentsTA.setRows(5);
        jScrollPane1.setViewportView(documentsTA);

        createButton.setFont(new java.awt.Font("Bitstream Vera Sans", 0, 16)); // NOI18N
        createButton.setText("Guardar");
        createButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createButtonActionPerformed(evt);
            }
        });

        cancelButton.setFont(new java.awt.Font("Bitstream Vera Sans", 0, 16)); // NOI18N
        cancelButton.setText("Cancelar");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 661, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 23, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(periodLbl)
                                    .addComponent(typeLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(44, 44, 44)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(periodTF, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(typeCB, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(53, 53, 53)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(createButton, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(documentsLbl))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(54, 54, 54)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(typeLbl)
                    .addComponent(typeCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(createButton))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(periodTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(periodLbl))
                        .addGap(72, 72, 72)
                        .addComponent(documentsLbl))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addComponent(cancelButton)))
                .addGap(30, 30, 30)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(247, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

      private String getDateTime(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");       
        return dateFormat.format(date);
    }
    
    private void createButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createButtonActionPerformed
        RequestVO request = new RequestVO();
        request.setRequestType(typeCB.getSelectedItem().toString());
        request.setDate(getDateTime(date));
        request.setAcademicPeriod(periodTF.getText());
        request.setSupportDocs(documentsTA.getText());        
        request.setStudentIdentification(student.getDocument());
        request.setProgram(student.getProgram());
        request.setDecision("Nula");
        request.setInstance("Coordinación curricular");
        
        int result  = new RequestControl().createRequest(request, student);
        
        if(result == 0){
            parent.requestTable(student);            
        }else if(result  == -1){
          JOptionPane.showMessageDialog(this, "El tipo de solicitud no es válido", "Tipo inválido", 
                  JOptionPane.INFORMATION_MESSAGE);
        }else if(result == -2){
            JOptionPane.showMessageDialog(this, "La instancia de la solicitud no es válida", "Instancia inválida", 
                  JOptionPane.INFORMATION_MESSAGE);
        }else if(result == -3){
            JOptionPane.showMessageDialog(this, "La decisiön de la solicitud no es válida", "Decisión inválida", 
                  JOptionPane.INFORMATION_MESSAGE);
        }else if(result == -4){
           JOptionPane.showMessageDialog(this, "El periodo académico  no es válido", "Periodo académico inválido", 
                  JOptionPane.INFORMATION_MESSAGE);
        }else if(result == -5){
            JOptionPane.showMessageDialog(this, "Los documentos de soporte ingresados no son válidos", "documentos inválidos", 
                  JOptionPane.INFORMATION_MESSAGE);
        }else if(result == -6){
            JOptionPane.showMessageDialog(this, "Ha ocurrido un error", "Error Inesperado", 
                  JOptionPane.ERROR_MESSAGE);
        }
        
    }//GEN-LAST:event_createButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        parent.createRequest();
    }//GEN-LAST:event_cancelButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton createButton;
    private javax.swing.JLabel documentsLbl;
    private javax.swing.JTextArea documentsTA;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel periodLbl;
    private javax.swing.JTextField periodTF;
    private javax.swing.JComboBox typeCB;
    private javax.swing.JLabel typeLbl;
    // End of variables declaration//GEN-END:variables
}
